from django.db import models


class Department(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=200)
    detail = models.TextField(default='')

    class Meta:
        db_table = 'department'
        ordering = ['id']


class DepartmentRole(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=200)
    did = models.ForeignKey('Department', to_field='id', on_delete=models.CASCADE, db_column='did')

    class Meta:
        db_table = 'department_role'
        ordering = ['id']


class DepartmentPermission(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=200)
    detail = models.CharField(max_length=200)

    class Meta:
        db_table = 'department_permission'
        ordering = ['id']


class Work(models.Model):
    uid = models.ForeignKey('user.User', to_field='id', on_delete=models.CASCADE, db_column='uid')
    rid = models.ForeignKey('DepartmentRole', to_field='id', on_delete=models.PROTECT, db_column='rid')
    annotation = models.CharField(max_length=200)

    class Meta:
        db_table = 'work'
        constraints = [
            models.UniqueConstraint(fields=['uid', 'rid'], name='work_id')
        ]


class DepartmentParticipate(models.Model):
    pid = models.ForeignKey('project.Project', to_field='id', on_delete=models.CASCADE, db_column='pid')
    did = models.ForeignKey('Department', to_field='id', on_delete=models.CASCADE, db_column='did')

    class Meta:
        db_table = 'department_participate'
        constraints = [
            models.UniqueConstraint(fields=['pid', 'did'], name='department_participate_id')
        ]


class DepartmentEmpower(models.Model):
    rid = models.ForeignKey('DepartmentRole', to_field='id', on_delete=models.CASCADE, db_column='rid')
    pid = models.ForeignKey('DepartmentPermission', to_field='id', on_delete=models.CASCADE, db_column='pid')

    class Meta:
        db_table = 'department_empower'
        constraints = [
            models.UniqueConstraint(fields=['rid', 'pid'], name='department_empower_id')
        ]
